<?php

class ArticleController extends FrontendController
{
    /**
     * @param $id
     * @throws CHttpException
     */
    public function actionIndex($cat)
    {
        if(!$cat)
            throw new CHttpException(404,'The requested page does not exist.');

        $categoryModel = Category::model()->findByAttributes(array('alias'=>$cat));
        if(!$categoryModel)
            throw new CHttpException(404,'The requested page does not exist.');
        //var_dump($categoryModel);
        //获取分类模板和分页数量
        $categoryTemplate = $categoryModel->template ? $categoryModel->template : 'list';
        $categoryPageSize = $categoryModel->page_size ? $categoryModel->page_size : 10;
        $seoTitle = $categoryModel->name.' - '.$this->_websiteTitle;
        $seoKeywords = $categoryModel->keywords ? $categoryModel->keywords : $this->_seoKeywords;
        $seoDescription = $categoryModel->description ? $categoryModel->description : $this->_seoDescription;

        $allCat = Category::getDescendants($categoryModel->id); //获取所有子分类信息
        $cidAll = '';
        foreach ($allCat as $k=>$v){
            if($k == 0){
                $cidAll .= $v['id'];
            }else{
                $cidAll .= ','.$v['id'];
            }
        }
        if ($allCat){
            $cidAll = $cidAll;
        }else {
            $cidAll = $categoryModel->id;
        }
        $criteria = new CDbCriteria();
        $criteria->order = 't.sort ASC, t.id DESC';
        $criteria->condition = 't.status = '.Article::ARTICLE_PUBLISHED;
        $criteria->condition .= ' AND t.type = '.Article::ARTICLE;
        $criteria->condition .= ' AND cid in ('. $cidAll.')';
        $pages = new CPagination($pageCount);
        $pages->pageSize = $categoryPageSize;
        $pages->applyLimit($criteria);
        $model = Article::model()->with('articleCategory')->findAll($criteria);
        //var_dump($criteria);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        $this->render($categoryTemplate,array(
            'model'=>$model,
            'pager'=>$pages,
            'seoTitle'=>$seoTitle,
            'seoKeywords'=>$seoKeywords,
            'seoDescription'=>$seoDescription,
            'category'=>$categoryModel, //获取分类信息
            'hotArticles'=>Article::getHotArticles(10), //热门文章
            'recommendArticles'=>Article::getRecommendArticles(10), //推荐文章
        ));
    }

    /**
     * @param $id
     * @throws CHttpException
     */
	public function actionView($id)
	{
        $model=Article::model()->findByPk($id);
        if($model===null)
            throw new CHttpException(404,'The requested page does not exist.');
        $categoryModel = Category::model()->findByAttributes(array('id'=>$model->cid));
        $articleTemplate = $categoryModel->article_template ? $categoryModel->article_template : 'show';

		$this->render($articleTemplate, array(
			'model'=>$this->loadModel($id),
			'category'=>$categoryModel, //分类信息
            'preArticle'=>Article::getPreArticle($id), //上一篇
            'nextArticle'=>Article::getNextArticle($id), //下一篇
            'hotArticles'=>Article::getHotArticles(10), //热门文章
            'recommendArticles'=>Article::getRecommendArticles(10), //推荐文章
		));
	}

	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer $id the ID of the model to be loaded
	 * @return ArticleMeta the loaded model
	 * @throws CHttpException
	 */
	public function loadModel($id)
	{
		$model=Article::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param Article $model the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='article-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
}
